/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "Curve3D.h"
#include "MathParser.h"
#include "Expression3D.h"
#include "Parameter.h"
#include "CurveSegment3D.h"
#include "Project.h"
#include <cfloat>
#include <GL/gl.h>


Curve3D::Curve3D(QObject *parent)
    :ParametricObject3D(parent),_segment(0){

}

Curve3D::Curve3D(const Expression3D *expression, QObject *parent)
    :ParametricObject3D(expression, parent),_segment(0){
}

Curve3D::Curve3D(const QString& expressionName, QObject *parent)
    :ParametricObject3D(expressionName, parent),_segment(0){
}


void Curve3D::compute() {
    cleanUpPoints();
    if(!isValid())
        return;
    _segment = new CurveSegment3D(_expression,this);
    _segment->setColor(_color);
    QList<Parameter*>::iterator it;
    for(it=_parameters.begin();it!=_parameters.end();++it)
        _segment->updateParameter(*it);
    _segment->compute();
    _xMin = _segment->xMin();
    _yMin = _segment->yMin();
    _zMin = _segment->zMin();
    _xMax = _segment->xMax();
    _yMax = _segment->yMax();
    _zMax = _segment->zMax();
    reset();
}

void Curve3D::draw(){
    glColor3f(_color.redF(),_color.greenF(),_color.blueF());
    if(_segment){
        _segment->draw();
    }

}

Object *Curve3D::clone() const{
    return new Curve3D(_expression);
}

void Curve3D::cleanUpPoints(){
    if(_segment)
        delete _segment;
    _segment = 0;
}

void Curve3D::cleanUp(){
    ParametricObject3D::cleanUp();
    cleanUpPoints();
}

bool Curve3D::isValid() const {
    if(ParametricObject3D::isValid()){
       if(autoParameters().count()==1)
            return true;
    }
    return false;
}

bool Curve3D::isEmpty() const {
    if(_segment)
        return _segment->isEmpty();
    return true;
}\

int Curve3D::type() const {
    return Object::Curve;
}


int Curve3D::count() const{
    if(_segment)
        return _segment->count();
    return 0;
}
